# 数据库工具函数集合
import pymysql

from domain.medicalRecord import MedicalRecord
from domain.patient import Patient

db = pymysql.connect(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    database="medical_record",
    charset="utf8"
)


# 连接数据库，返回患者所有信息查询
def Patient_Query_All():
    cursor = db.cursor()
    sql = "select * from patients"
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        return results
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return None


# 患者登录
def Patient_Login(card_id: str, password: str):
    cursor = db.cursor()
    # 使用参数化查询防止 SQL 注入
    sql = "SELECT password FROM patients WHERE card_id = '%s'" % card_id
    try:
        cursor.execute(sql)
        results = cursor.fetchone()
        print(results[0])
        if results is not None and results[0] == password:
            return True
        else:
            return False
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return False


# 患者注册
def Patient_Register(patient: Patient):
    cursor = db.cursor()
    sql = "INSERT INTO patients (card_id, name, age, gender, phone,password) VALUES (%s, %s, %s, %s, %s, %s)"
    value = (patient.card_id, patient.name, patient.age, patient.gender, patient.phone,patient.password)
    if (patient.card_id=='' or patient.name=='' or patient.age=='' or patient.gender=='' or patient.phone==''
            or patient.password==''):
        print("请输入完整信息")
        return False
    try:
        cursor.execute(sql, value)
        db.commit()
        return True
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return False


# 医生登录
def Doctor_Login(doctor_id: str, password: str):
    cursor = db.cursor()
    # 使用参数化查询防止 SQL 注入
    sql = "SELECT password FROM doctors WHERE doctor_id = '%s'" % doctor_id
    try:
        cursor.execute(sql)
        results = cursor.fetchone()
        if results is not None and results[0] == password:
            return True
        else:
            return False
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return False


# 查询所有病历单
def MedicalRecord_Query_All():
    cursor = db.cursor()
    sql = "select * from medical_records"
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        return results
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return None


# 添加病历单
# 修改 MedicalRecord_Add 函数
def MedicalRecord_Add(record: MedicalRecord):
    cursor = db.cursor()
    sql = ("insert into medical_records(patient_card_id, patient_name, patient_age, patient_gender, diagnosis_info, "
           "doctor_name, doctor_id, doctor_advice) values(%s, %s, %s, %s, %s, %s, %s, %s)")
    values = (record.patient_card_id, record.patient_name, record.patient_age, record.patient_gender,
              record.diagnosis, record.doctor_name, record.doctor_id, record.advice)
    try:
        cursor.execute(sql, values)
        db.commit()
        return True
    except Exception as e:
        print(f"Error: unable to insert data. Details: {e}")
        return False
